﻿package Cain.game.clickFun 
{
	import Cain.game.clickFun.clickMc.BalloonMC;
	import Cain.game.clickFun.clickMc.StoneMC;
	import Cain.game.clickFun.clickMc.BirdMc;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.getDefinitionByName;
	import flash.utils.Timer;
	/**
	 * 點點樂場景 v1.1
	 * 場景上的物件備擊點後會回傳事件,自己本身的class 名子
	 * @author Cain
	 */
	public class ClickFunStage extends Sprite {
		private var ww:Number; //寬
		private var hh:Number; //高
		private var balloonSP:Sprite; //氣球圖層
		private var stoneSP:Sprite; //炸彈圖層
		/**
		 * 氣球上升加速
		 */
		public var balloon_up_v:Number = 0; 
		/**
		 * 分數
		 */
		public var score:int;
		private var balloonMCTimer:Array; //timer陣列
		/**
		 * 點點樂舞場景
		 * @param	_ww 寬
		 * @param	_hh 高
		 */
		public function ClickFunStage(_ww:Number,_hh:Number) 
		{
			ww = _ww;
			hh = _hh;
			setup();
		}
		private function setup(_time:int = 30):void
		{
			balloonMCTimer = new Array();
			addEventListener(Event.ADDED_TO_STAGE, addthis);
		}
		
		private function addthis(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addthis);
			addEventListener(Event.REMOVED_FROM_STAGE, renovethis);
			init();
		}
		
		private function init():void
		{
			balloonSP = new Sprite();
			addChild(balloonSP);
			stoneSP = new Sprite();
			addChild(stoneSP);
		}
		/**
		 * 發射氣球物件
		 * @param	_balloonMCname 氣球視覺物件
		 * @param	_score 分數
		 * @param	_generation 發射器生成速度
		 * @param	_up_v 氣球初速
		 * @param	_offset 氣球偏移
		 */
		public function addballoonMC(_balloonMCname:String,_score:int=1,_generation:int = 1000,_up_v:Number=3,_offset:Number=1):void 
		{
			var timer:Timer = new Timer(_generation);
			balloonMCTimer.push(timer);
			timer.addEventListener(TimerEvent.TIMER, onTIMER);
			function onTIMER(e:TimerEvent):void 
			{
				var ball:BalloonMC = new BalloonMC(_balloonMCname, _up_v+balloon_up_v, _offset);
				ball.scaleX = ball.scaleY = Math.random() * .4 + .8;
				ball.x = Math.random()*ww/2+ww/4;
				ball.y = hh + ball.height;
				balloonSP.addChild(ball);
				ball.addEventListener(MouseEvent.CLICK, ballonCLICK);
				function ballonCLICK(e:MouseEvent):void 
				{
					ball.removeEventListener(MouseEvent.CLICK, ballonCLICK);
					score += _score;
					/**
					 * 汽球備擊點
					 */
					dispatchEvent(new Event(_balloonMCname));
				}
			}
			
		}
		/**
		 * 炸彈拋物發射器
		 * @param	_MCname 氣球視覺物件
		 * @param	_generation 發射器生成速度
		 * @param	_g 重力
		 */
		public function addStomeMC(_MCname:String,_generation:int = 2000,_g:Number = 1):void 
		{
			var timer:Timer = new Timer(_generation);
			balloonMCTimer.push(timer);
			timer.addEventListener(TimerEvent.TIMER, onTIMER);
			function onTIMER(e:TimerEvent):void 
			{
				var stone:StoneMC = new StoneMC(_MCname, _g);
				stone.x = Math.random()*ww;
				stone.y = hh + stone.height;
				stone._vx = (ww/2 - stone.x)/20;
				//stone._vx = 0; 
				stone._vy = Math.random()*-10-20; 
				stoneSP.addChild(stone);
				stone.addEventListener(MouseEvent.CLICK, stoneonCLICK);
				function stoneonCLICK(e:MouseEvent):void 
				{
					stone.removeEventListener(MouseEvent.CLICK, stoneonCLICK);
					score = 0; //分數歸零
					dispatchEvent(new Event(_MCname));
				}
			}
			
		}
		/**
		 * 飛行障礙發射器
		 * @param	_birdName 飛行器視覺物件
		 * @param	_generation 發射器生成速度
		 * @param	max_s 飛躍一次最大秒數
		 * @param	min_s 飛躍一次最小秒數
		 * @param	foot_y 最低飛行座標
		 * @param	top_y 最高飛行座標
		 */
		public function addBirdMC(_birdName:String, _generation:int = 2000,max_s:Number = 10, min_s:Number = 5, foot_y:Number = 200, top_y:Number = 0)
		{
			var timer:Timer = new Timer(_generation);
			balloonMCTimer.push(timer);
			timer.addEventListener(TimerEvent.TIMER, onTIMER);
			function onTIMER(e:TimerEvent):void 
			{
				var bird:BirdMc = new BirdMc(_birdName, 0, Math.random() * (foot_y - top_y) + top_y, ww, Math.random() * (foot_y - top_y) + top_y, Math.random() * (max_s - min_s) + min_s);
				bird.scaleX = bird.scaleY = Math.random()*.4 + .8;
				stoneSP.addChild(bird);
				bird.addEventListener(MouseEvent.CLICK, birdonCLICK);
				function birdonCLICK(e:MouseEvent):void 
				{
					bird.removeEventListener(MouseEvent.CLICK, birdonCLICK);
					score = 0; //分數歸零
					dispatchEvent(new Event(_birdName));
				}
			}
		}
		/**
		 * 開始遊戲
		 */
		public function start():void 
		{
			score = 0;
			for each (var timer:Timer in balloonMCTimer) 
			{
				timer.start();
			}
		}
		/**
		 * 開始遊戲
		 */
		public function stop():void 
		{
			for each (var timer:Timer in balloonMCTimer) 
			{
				timer.stop();
			}
		}
		private function renovethis(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, renovethis);
		}
	}
	
}